# -*- coding: utf-8 -*-
#
# The Philatelist program is a fork of the OpenNumismat program
# Copyright (c) 2011 - 2024 Vitaly Ignatov (OpenNumismat) <opennumismat@gmail.com>
# Copyright (c) 2021 - 2024 Victor Nabatov (Philatelist) <nabatov.victor@gmail.com>
#

from PySide6.QtCore import Qt
from PySide6.QtWidgets import (
    QAbstractButton, QGridLayout, QGroupBox, QHBoxLayout, QSizePolicy, QTextEdit, QWidget
)

from Philatelist.Collection.CollectionFields import TraitsFields, FieldTypes as Type


class BaseFormLayout(QGridLayout):

    def __init__(self):
        super().__init__()
        self.row = 0
        self.columnCount = 5

    def isEmpty(self):
        return self.row == 0

    def addRow(self, item1, item2=None):
        if not item2:
            if item1.isHidden():
                return

            widget = item1.widget()
            if isinstance(widget, QTextEdit):
                self.addWidget(item1.label(), self.row, 0, 1, self.columnCount, Qt.AlignCenter | Qt.AlignTop)
                item1.label().setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed)
                item1.label().setStyleSheet("color: blue")
                self.row += 1
                self.addWidget(widget, self.row, 0, 1, self.columnCount)
                widget.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
            else:
                item1.label().setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
                self.addWidget(item1.label(), self.row, 0)
                # NOTE: columnSpan parameter in addWidget don't work with value -1 for 2-columns grid
                # self.addWidget(widget, self.row, 1, 1, -1)
                if item1.type() == Type.Number:
                    widget.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)

                self.addWidget(widget, self.row, 1, 1, self.columnCount - 1)
        else:
            if item1.isHidden() and item2.isHidden():
                return

            if isinstance(item2, QAbstractButton):
                if item1.isHidden():
                    return

                self.addWidget(item1.label(), self.row, 0)
                self.addWidget(item1.widget(), self.row, 1, 1, 3)
                self.addWidget(item2, self.row, 4)
            else:
                col = 0
                if not item1.isHidden():
                    self.addWidget(item1.label(), self.row, col)
                    if item1.type() in (Type.BigInt, Type.Status):
                        item1.widget().setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)

                    col += 1
                    if self.columnCount == 6:
                        self.addWidget(item1.widget(), self.row, col, 1, self.columnCount - 4)
                    else:
                        self.addWidget(item1.widget(), self.row, col)

                    col += 2

                if not item2.isHidden():
                    self.addWidget(item2.label(), self.row, col)
                    col += 1
                    self.addWidget(item2.widget(), self.row, col)

        self.row = self.row + 1

    def addHalfRow(self, item1):
        if item1.isHidden():
            return

        col = 0
        self.addWidget(item1.label(), self.row, col)
        col += 1
        self.addWidget(item1.widget(), self.row, col, Qt.AlignLeft)
        col += 1
        widget = QWidget()
        self.addWidget(widget, self.row, col)
        widget.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed)
        col += 1
        self.row = self.row + 1


class BaseFormGroupBox(QGroupBox):

    def __init__(self, title):
        super().__init__(title)
        self.layout = BaseFormLayout()
        self.setLayout(self.layout)
        self.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed)

    def isEmpty(self):
        return self.layout.isEmpty()

    def addRow(self, item1, item2=None):
        if not item2:
            if item1.isHidden():
                return

            if item1._field in ('watermark', 'grid', 'honeycomb', 'raster'):
                item1.label().setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed)
                self.layout.addWidget(
                    item1.label(),
                    self.layout.row,
                    0, 1,
                    self.layout.columnCount,
                    Qt.AlignCenter
                )
                self.layout.row += 1

                item1.widget().setMinimumWidth(300)
                self.layout.addWidget(
                    item1.widget(),
                    self.layout.row,
                    0, 1,
                    self.layout.columnCount,
                    Qt.AlignCenter
                )
                self.layout.row += 1

            else:
                self.layout.addRow(item1, item2)
                self.fixSizePolicy(item1)

        else:
            self.layout.addRow(item1, item2)
            self.fixSizePolicy(item1)
            if item2 and not isinstance(item2, QAbstractButton):
                self.fixSizePolicy(item2)

    def addHalfRow(self, item1):
        self.layout.addHalfRow(item1)

    def fixSizePolicy(self, item):
        if not item.isHidden() and item.type() == Type.Text:
            self.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)


class DesignFormLayout(BaseFormGroupBox):

    def __init__(self, title, params=None):
        super().__init__(title)
        self.layout.columnCount = 5
        self.defaultHeight = 20

    def addImage(self, image, rowSpan=-1):
        if not image.isHidden():
            self.layout.addWidget(image.widget(), 0, 5, 4, 1)
            image.widget().setFixedSize(200, 200)

    def isEmpty(self):
        return super().isEmpty()

    def addRow(self, item1, item2=None):
        if item2 and not item2.isHidden() and not item1.isHidden():
            if item1._field in TraitsFields:
                self.layout.columnCount = 5
                widget = QWidget()
                item1.label().setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Minimum)
                self.layout.addWidget(
                    item1.label(),
                    self.layout.row, 0,
                    1, self.layout.columnCount-2,
                    Qt.AlignCenter
                )
                self.layout.row += 1

                self.layout.addWidget(widget, self.layout.row, 0, 1, 1)
                item1.widget().setMinimumWidth(300)
                self.layout.addWidget(item1.widget(), self.layout.row, 1, 1, 1)
                self.layout.row += 1

                item2.widget().setFixedSize(130, 130)
                item2.widget().setContextMenuPolicy(Qt.NoContextMenu)
                self.layout.addWidget(item2.widget(), 0, 5, 2, self.layout.columnCount-3)
                self.layout.row += 1

            elif item1._field == 'designer':
                self.layout.addWidget(item1.label(), self.layout.row, 0)
                self.layout.addWidget(item1.widget(), self.layout.row, 1, 1, 1)
                item1.widget().setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed)
                self.layout.addWidget(item2.label(), self.layout.row, 2)
                item2.label().setMinimumWidth(50)
                self.layout.addWidget(item2.widget(), self.layout.row, 3, 1, 1)
                item2.widget().setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed)
                self.layout.row += 1
            else:
                self.layout.addWidget(item1.label(), self.layout.row, 0)
                self.layout.addWidget(item1.widget(), self.layout.row, 1, 1, 1, Qt.AlignLeft)
                item1.widget().setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed)
                self.layout.addWidget(item2.label(), self.layout.row, 2)
                item2.label().setMaximumWidth(70)
                item2.label().setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
                self.layout.addWidget(item2.widget(), self.layout.row, 3, 1, 1, Qt.AlignLeft)
                item2.widget().setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed)
                self.layout.row += 1

        elif isinstance(item1.widget(), QTextEdit):
                self.layout.addWidget(item1.label(), self.layout.row, 0, 1, self.layout.columnCount - 1, Qt.AlignCenter | Qt.AlignTop)
                item1.label().setStyleSheet("color: blue")
                self.layout.row += 1
                self.layout.addWidget(item1.widget(), self.layout.row, 0, 1, self.layout.columnCount - 1)
                item1.widget().setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
                self.layout.row += 1
        else:
            super().addRow(item1, item2)

    def addHalfRow(self, item1):
        if item1.isHidden():
            return

        self.layout.addWidget(item1.label(), self.layout.row, 0)
        hlayout = QHBoxLayout()
        hlayout.addWidget(item1.widget())

        widget = QWidget()
        widget.setMinimumWidth(0)
        widget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
        hlayout.addWidget(widget)

        self.layout.addLayout(hlayout, self.layout.row, 1, 1, -1)
        self.layout.row += 1


class ImageFormLayout(BaseFormLayout):

    def __init__(self):
        super().__init__()
        self.imagesCount = 0
        self.row = 0

    def addRow(self, item1, item2=None):
        if item2 and not item2.isHidden() and not item1.isHidden():
            self.addWidget(item1.label(), self.row, 0)
            self.addWidget(item1.widget(), self.row, 1)
            self.addWidget(item2.label(), self.row, 2)
            self.addWidget(item2.widget(), self.row, 3)
            self.row += 4

    def addImages(self, images):
        col = 0
        for image in images:
            if not image.isHidden():
                row = 1 + self.imagesCount // 2

                self.addWidget(image.widget(), row * 2 + 1, col, 1, 2)
                self.setRowMinimumHeight(row * 2 + 1, 120)
                self.setColumnMinimumWidth(col, 160)
                self.imagesCount += 1
                col += 2
                if col == 4:
                    col = 0

    def isEmpty(self):
        return (self.imagesCount == 0)

